//
//  MJTEmoticonTools.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/20.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit

class MJTEmoticonTools: NSObject {
    // 定义单例全局访问点
    static let shareTools = MJTEmoticonTools()
    //构造函数私有化
    private override init() {
        super.init()

    }
    //创建bundle对象
    private lazy var emoticonBundle: NSBundle = {
        //获取路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!
        //创建bundle对象
        let bundle = NSBundle(path: path)!
        return bundle
    }()
    // 读取默认表情数据
    private lazy var defaultEmoticonArray: [MJTEmoticon] = {
        return self.loadEmoticonsWithPath("default/info.plist")
    }()
    //读取emoji表情
    private lazy var emojiEmoticonArray: [MJTEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    //读取浪小花表情
    private lazy var lxhEmoticonArray: [MJTEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    //读取表情通用方法,根据子路经
    private func loadEmoticonsWithPath(subPath:String) -> [MJTEmoticon] {
        //获取bundle对象
        let bundle: NSBundle = self.emoticonBundle
        //获取info.plist文件路径
        let path = bundle.pathForResource(subPath, ofType: nil)!
        //获取plist文件里面的内容
        let emoticonDicArray = NSArray(contentsOfFile: path)!
        var tempArray = [MJTEmoticon]()
        //遍历数组转模型
        for emoticonDic in emoticonDicArray {
            let emoticon = MJTEmoticon(dic: emoticonDic as! [String : AnyObject])
            //如果是图片需要处理路径
            if emoticon.type == "0" {
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emoticon.path = lastPath + "/" + emoticon.png!
            }
            tempArray.append(emoticon)
        }
        return tempArray
    }
    //把表情数据拆分成二维数组
    private func pagesWithEmotions(emoticon: [MJTEmoticon]) -> [[MJTEmoticon]] {
        // 根据图片的个数计算页数
        let pages = (emoticon.count - 1) / numbersOfPage + 1
        var tempArray = [[MJTEmoticon]]()
        for i in 0..<pages {
            //截取的索引
            let loc = i * numbersOfPage
            //截取的个数
            var len = numbersOfPage
            //如果截取长度不够则截取到剩余个数
            if loc + len > emoticon.count {
                len = emoticon.count - loc
            }
            let subArray = (emoticon as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [MJTEmoticon]
            tempArray.append(subArray)
        }
        return tempArray
    }
    //准备表情视图需要的数据
    lazy var allWmoticonArray: [[[MJTEmoticon]]] = {
        return [
                    self.pagesWithEmotions(self.defaultEmoticonArray),
                    self.pagesWithEmotions(self.emojiEmoticonArray),
                    self.pagesWithEmotions(self.lxhEmoticonArray)
               ]
    }()
    //根据表情描述字符串查找表情模型
    func selectEmoticonWithChs(chs: String) -> MJTEmoticon? {
        //第一种方法
        //在默认表情和浪小花表情数组查找,原有emoji不是图片
        for value in defaultEmoticonArray {
            if value.chs! == chs {
                return value
            }
        }
        for value in lxhEmoticonArray {
            if value.chs! == chs {
                return value
            }
        }
        return nil
    }

}
